package com.example.administrator.ttphonehelper.contact;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.ttphonehelper.R;
import com.example.administrator.ttphonehelper.base.SimpleBaseAdapter;
import com.example.administrator.ttphonehelper.bean.ContactBean;
import com.example.administrator.ttphonehelper.util.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/4/22 0022.
 */
public class ContactAdapter extends SimpleBaseAdapter<ContactBean> implements View.OnClickListener {
    private Context mContext;

    public ContactAdapter(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public View getItemView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            view = mInflater.inflate(R.layout.contact_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        ContactBean bean = getItem(position);
        holder.name.setText(bean.getName());
        holder.number.setText(bean.getMobileTel());

        if (bean.getPhoto() != null) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(bean.getPhoto(), 0, bean.getPhoto().length);
            holder.photo.setImageBitmap(bitmap);
        } else {
            holder.photo.setImageResource(R.drawable.contact_person);
        }
        //添加点击事件
        holder.call.setOnClickListener(this);
        holder.call.setTag(bean.getMobileTel());
        holder.sendSms.setOnClickListener(this);
        holder.sendSms.setTag(bean.getMobileTel());
        holder.edit.setOnClickListener(this);
        holder.edit.setTag(bean);
        return view;
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.iv_call:
                intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + v.getTag()));
                if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                mContext.startActivity(intent);
                break;
            case R.id.iv_send_sms:
                intent = new Intent(Intent.ACTION_VIEW);
                intent.setType("vnd.android-dir/mms-sms");
                intent.putExtra("address", v.getTag().toString());
                mContext.startActivity(intent);

                break;
            case R.id.iv_edit:
                intent = new Intent(mContext,ContactUpdateActivity.class);
                intent.putExtra("contact",(ContactBean)v.getTag());
                ((ContactActivity)mContext).startActivityForResult(intent,ContactActivity.REQUEST_CODE_UPDATE);
                break;
        }
    }

    static class ViewHolder{

        @Bind(R.id.tv_name) TextView name;
        @Bind(R.id.tv_number) TextView number;
        @Bind(R.id.iv_icon) ImageView photo;
        @Bind(R.id.iv_call) ImageView call;
        @Bind(R.id.iv_send_sms) ImageView sendSms;
        @Bind(R.id.iv_edit) ImageView edit;
        public ViewHolder(View view) {
            ButterKnife.bind(this,view);
        }
    }

}
